//http://docs.oracle.com/javase/7/docs/api/

import java.io.*;
import java.util.*;

public class Main{  
     
    public static void main(String[] args) {  

	final int _TX_			=0;
	final int _RX_			=1;

	final int _Tag_			=2;

	final int _DelayFirst_		=3;
	final int _DelayLast_		=4;

	final int _RepeatOk_		=5;
	final int _RepeatFail_		=6;

	final int _JmpOkFirst_		=7;
	final int _JmpOkLast_		=8;

	final int _JmpFailLast_		=9;
	final int _JmpFailFirst_	=10;

	final int _MaxLen_		=11;
	final int _Padding_		=12;

	final int _BaudRate_		=13;
	final int _TimeOut_		=14;

	final int _Jmp_			=15;


	final int _ArraySize_		=16;//put in the last

	String[] rbuf=new String[_ArraySize_];

	int dbg=0x08;
	final int _DBGparseString_	=0x01;
	final int _DBGTxRx_		=0x02;
	final int _DBGOther_		=0x04;
	final int _DBGUARTTxRx_		=0x08;

	DBG dbgmessage=new DBG("main.dbg",0x03);
//	_ShowDeltaTime_	=0x01;
//	_ShowOnScreen_	=0x02;


	String[] comList;
	String[] allString;
	ComPort comport=new ComPort(0x200);
	int select=0;
	int defaultselect=0;
	int baudRate=115200;
	int defaultBaudRate=115200;
	int TmpInt=0;
	
	String srcfile="a.txt";
//	byte[] TxData=new byte[0x800];
	String seperateString="\n";
	String[] removeString={"\t"," ","\r\n","\n","\r"};
	String[] newremoveString;
	int index=0;

	String[][] db;
	String 	TmpString	="";

	byte[]	Tx		={},
		Rx		={},
		TmpRx		={};
	byte	CRCCheck;
	int	intCRC;
	Date date=new Date();
	int oldtime=0;
	
	FileExe readfile=new FileExe();

	StringProcess stringProcess=new StringProcess();



	for(int i=0;i<args.length;i++){

		switch(args[i]){
			case "-s"://src file
				i++;
				srcfile=args[i];
				break;

			case "-v":
				System.out.println("================= Version ===============");
//				System.out.println("=========================================");
				System.out.println("2013/08/10");
				break;
			case "-h"://help
				System.out.println("================= Help ==================");
				System.out.println("-s   :source file");
				System.out.println("-com :default com port");
				System.out.println("-baud:default baudrate");
				System.out.println("-h   :for help");
				System.out.println("delay unit=1ms");
				System.out.println();
				System.out.println("ex:-com 5 -baud 921600");
				break;
			case "-com":
				i++;
				defaultselect=Integer.parseInt(args[i]);
				break;
			case "-baud":
				i++;
				defaultBaudRate=Integer.parseInt(args[i]);
				break;
			case "-rm":
				i++;
				int j=0;
				newremoveString=new String[removeString.length+1];
				for(j=0;j<removeString.length;j++)
					newremoveString[j+1]=removeString[j];
//args[i]="\\r\\n:";
//args[i]="\\n:";
				newremoveString[0]=args[i];

				if(newremoveString[0].indexOf("\\r")!=-1){
//dbgmessage.save("args[i]="+args[i].indexOf("\\r"));
					newremoveString[0].replace("\\r","\r");
				}
				if(newremoveString[0].indexOf("\\n")!=-1){
//dbgmessage.save("args[i]="+args[i].indexOf("\\n"));
					newremoveString[0].replace("\\n","\n");
				}
				if(newremoveString[0].indexOf("\\t")!=-1){
//dbgmessage.save("args[i]="+args[i].indexOf("\\t"));
					newremoveString[0].replace("\\t","\t");
				}

//dbgmessage.save("newremoveString[0]="+newremoveString[0]);

				removeString=new String[newremoveString.length];
				removeString=newremoveString;

				break;
			default:
				System.out.println("useless parameter:"+args[i]);
				break;
		}
	}



	try{


		allString=stringProcess.parseString(readfile.readString(srcfile),seperateString,removeString);


		for(int k=0;k<allString.length;k++){

			if(allString[k].length()!=0) {
				Tx=stringProcess.stringToByte(allString[k]);
				CRCCheck=0;
				for(int m=0;m<Tx.length;m++){
					CRCCheck+=Tx[m];
				}
				Tx=new byte[1];
				Tx[0]=CRCCheck;
dbgmessage.save("["+k+"]");

dbgmessage.save(allString[k]+"\talladd:"+stringProcess.byteToString(Tx));

				CRCCheck=(byte)(256-CRCCheck);
				Tx[0]=CRCCheck;
dbgmessage.save("\tCRC:"+stringProcess.byteToString(Tx));
dbgmessage.save("-----------------------------------------");
			}
		}

























//TX RX test--------------------------------------------------------------------------------------------
/*
	//prcess string--------------------------------------------------------------------------------------------

		//record db-------------------------------------
		db=new String[allString.length][_ArraySize_];

System.out.println("== Test Item Number:"+allString.length+" =========");

		for(int k=0;k<allString.length;k++){

			allString[k]=allString[k].toUpperCase();
if((dbg&_DBGparseString_)==_DBGparseString_){
	TmpString=k+"\t:"+allString[k];
	dbgmessage.save(TmpString);
}

			//_TX_----------------------------------------------------------------------------
			TmpString=stringProcess.getPartOfString(allString[k],"Tx=".toUpperCase(),",");

			db[k][_TX_]="";

			if(TmpString!=null && TmpString!=""){
				if(TmpString.length()>0){
					db[k][_TX_]=TmpString;
				}
			}


if((dbg&_DBGparseString_)==_DBGparseString_){
	TmpString="\t\tdb["+k+"][_TX_]\t\t="+db[k][_TX_];
	dbgmessage.save(TmpString);
}

			//_RX_----------------------------------------------------------------------------
			TmpString=stringProcess.getPartOfString(allString[k],"Rx=".toUpperCase(),",");
			db[k][_RX_]="";

			if(TmpString!=null && TmpString!=""){
				if(TmpString.length()>0){
					db[k][_RX_]=TmpString;
				}
			}
if((dbg&_DBGparseString_)==_DBGparseString_){
	TmpString="\t\tdb["+k+"][_RX_]\t\t="+db[k][_RX_];
	dbgmessage.save(TmpString);
}
			//_Tag_----------------------------------------------------------------------------
			TmpString=stringProcess.getPartOfString(allString[k],"Tag=".toUpperCase(),",");

			db[k][_Tag_]="";

			if(TmpString!=null && TmpString!=""){
				if(TmpString.length()>0){
					db[k][_Tag_]=TmpString;
				}
			}

if((dbg&_DBGparseString_)==_DBGparseString_){
//dbgmessage.save("/"+TmpString+"/");
	TmpString="\t\tdb["+k+"][_Tag_]\t\t="+db[k][_Tag_];
	dbgmessage.save(TmpString);
}
			//_DelayFirst_----------------------------------------------------------------------------
			TmpString=stringProcess.getPartOfString(allString[k],"DelayFirst=".toUpperCase(),",");
			db[k][_DelayFirst_]="0";

			if(TmpString!=null && TmpString!=""){
				if(TmpString.length()>0){
					db[k][_DelayFirst_]=TmpString;
				}
			}
if((dbg&_DBGparseString_)==_DBGparseString_){
	TmpString="\t\tdb["+k+"][_DelayFirst_]\t="+db[k][_DelayFirst_];
	dbgmessage.save(TmpString);
}
			//_DelayLast_----------------------------------------------------------------------------
			TmpString=stringProcess.getPartOfString(allString[k],"DelayLast=".toUpperCase(),",");
	
			db[k][_DelayLast_]="0";

			if(TmpString!=null && TmpString!=""){
				if(TmpString.length()>0){
					db[k][_DelayLast_]=TmpString;
				}
			}
if((dbg&_DBGparseString_)==_DBGparseString_){
	TmpString="\t\tdb["+k+"][_DelayLast_]\t="+db[k][_DelayLast_];
	dbgmessage.save(TmpString);
}
			//_RepeatOk_----------------------------------------------------------------------------
			TmpString=stringProcess.getPartOfString(allString[k],"RepeatOk=".toUpperCase(),",");

			db[k][_RepeatOk_]="0";

			if(TmpString!=null && TmpString!=""){
				if(TmpString.length()>0){
					db[k][_RepeatOk_]=TmpString;
				}
			}
if((dbg&_DBGparseString_)==_DBGparseString_){
	TmpString="\t\tdb["+k+"][_RepeatOk_]\t="+db[k][_RepeatOk_];
	dbgmessage.save(TmpString);
}
			//_RepeatFail_----------------------------------------------------------------------------
			TmpString=stringProcess.getPartOfString(allString[k],"RepeatFail=".toUpperCase(),",");

			db[k][_RepeatFail_]="0";

			if(TmpString!=null && TmpString!=""){
				if(TmpString.length()>0){
					db[k][_RepeatFail_]=TmpString;
				}
			}
if((dbg&_DBGparseString_)==_DBGparseString_){
	TmpString="\t\tdb["+k+"][_RepeatFail_]\t="+db[k][_RepeatFail_];
	dbgmessage.save(TmpString);
}
			//_JmpOkFirst_----------------------------------------------------------------------------
			TmpString=stringProcess.getPartOfString(allString[k],"JmpOkFirst=".toUpperCase(),",");

			db[k][_JmpOkFirst_]="";

			if(TmpString!=null && TmpString!=""){
				if(TmpString.length()>0){
					db[k][_JmpOkFirst_]=TmpString;
				}
			}
if((dbg&_DBGparseString_)==_DBGparseString_){
	TmpString="\t\tdb["+k+"][_JmpOkFirst_]\t="+db[k][_JmpOkFirst_];
	dbgmessage.save(TmpString);
}
			//_JmpOkLast_----------------------------------------------------------------------------
			TmpString=stringProcess.getPartOfString(allString[k],"JmpOkLast=".toUpperCase(),",");

			db[k][_JmpOkLast_]="";

			if(TmpString!=null && TmpString!=""){
				if(TmpString.length()>0){
					db[k][_JmpOkLast_]=TmpString;
				}
			}
if((dbg&_DBGparseString_)==_DBGparseString_){
	TmpString="\t\tdb["+k+"][_JmpOkLast_]\t="+db[k][_JmpOkLast_];
	dbgmessage.save(TmpString);
}
			//_JmpFailFirst_----------------------------------------------------------------------------
			TmpString=stringProcess.getPartOfString(allString[k],"JmpFailFirst=".toUpperCase(),",");

			db[k][_JmpFailFirst_]="";

			if(TmpString!=null && TmpString!=""){
				if(TmpString.length()>0){
					db[k][_JmpFailFirst_]=TmpString;
				}
			}
if((dbg&_DBGparseString_)==_DBGparseString_){
	TmpString="\t\tdb["+k+"][_JmpFailFirst_]\t="+db[k][_JmpFailFirst_];
	dbgmessage.save(TmpString);
}
			//_JmpFailLast_----------------------------------------------------------------------------
			TmpString=stringProcess.getPartOfString(allString[k],"JmpFailLast=".toUpperCase(),",");

			db[k][_JmpFailLast_]="";

			if(TmpString!=null && TmpString!=""){
				if(TmpString.length()>0){
					db[k][_JmpFailLast_]=TmpString;
				}
			}
if((dbg&_DBGparseString_)==_DBGparseString_){
	TmpString="\t\tdb["+k+"][_JmpFailLast_]\t="+db[k][_JmpFailLast_];
	dbgmessage.save(TmpString);
}
			//_Jmp_----------------------------------------------------------------------------
			TmpString=stringProcess.getPartOfString(allString[k],"Jmp=".toUpperCase(),",");
			db[k][_Jmp_]="";

			if(TmpString!=null && TmpString!=""){
				if(TmpString.length()>0){
					db[k][_Jmp_]=TmpString;
				}
			}
if((dbg&_DBGparseString_)==_DBGparseString_){
	TmpString="\t\tdb["+k+"][_Jmp_]\t="+db[k][_Jmp_];
	dbgmessage.save(TmpString);
}

			//_MaxLen_----------------------------------------------------------------------------
			TmpString=stringProcess.getPartOfString(allString[k],"MaxLen=".toUpperCase(),",");

			db[k][_MaxLen_]="";

			if(TmpString!=null && TmpString!=""){
				if(TmpString.length()>0){
					db[k][_MaxLen_]=TmpString;
				}
			}
if((dbg&_DBGparseString_)==_DBGparseString_){
	TmpString="\t\tdb["+k+"][_MaxLen_]\t\t="+db[k][_MaxLen_];
	dbgmessage.save(TmpString);
}
			//_Padding_----------------------------------------------------------------------------
			TmpString=stringProcess.getPartOfString(allString[k],"Padding=".toUpperCase(),",");

			db[k][_Padding_]="FF";

			if(TmpString!=null && TmpString!=""){
				if(TmpString.length()>0){
					db[k][_Padding_]=TmpString;
				}
			}

if((dbg&_DBGparseString_)==_DBGparseString_){
	TmpString="\t\tdb["+k+"][_Padding_]\t="+db[k][_Padding_];
	dbgmessage.save(TmpString);
}
			//_TimeOut_----------------------------------------------------------------------------
			TmpString=stringProcess.getPartOfString(allString[k],"TimeOut=".toUpperCase(),",");

			db[k][_TimeOut_]="0";

			if(TmpString!=null && TmpString!=""){
				if(TmpString.length()>0){
					db[k][_TimeOut_]=TmpString;
				}
			}

if((dbg&_DBGparseString_)==_DBGparseString_){
	TmpString="\t\tdb["+k+"][_TimeOut_]\t="+db[k][_TimeOut_];
	dbgmessage.save(TmpString);
}
		}

		//execise db------------------------------------
		index=0;
		int tmpindex=0;
		String oldString="";

		do{

			tmpindex++;
			//_DelayFirst_-------------------------------------
			oldtime=(int)System.currentTimeMillis();
			while(((int)System.currentTimeMillis()-oldtime)<Integer.parseInt(db[index][_DelayFirst_]));


			//_TX_---------------------------------------------
			
			if(db[index][_TX_].length()!=0){

if((dbg&_DBGTxRx_)==_DBGTxRx_){
	TmpString="\tdb["+index+"][_TX_]\t:"+db[index][_TX_];
	dbgmessage.save(TmpString);
}
				//_Padding_------------------------------------	
				if(db[index][_MaxLen_].length()!=0){
					if(Integer.parseInt(db[index][_MaxLen_])>(db[index][_TX_].length()/2)){
						TmpInt=Integer.parseInt(db[index][_MaxLen_])-(db[index][_TX_].length()/2);
						for(int n=0;n<TmpInt;n++){
							db[index][_TX_]+=db[index][_Padding_];
						}

					}
				}

				Tx=stringProcess.stringToByte(db[index][_TX_]);
				//comport.reset();
				comport.getRxBuf(0x01);



if((dbg&_DBGTxRx_)==_DBGTxRx_){	
	TmpString="\t\tTx\t:";
	TmpString+=stringProcess.byteToString(Tx);
	dbgmessage.save(TmpString);
}
if((dbg&_DBGUARTTxRx_)==_DBGUARTTxRx_){
	TmpString="["+index+"]\tUART Tx:";
	TmpString+=stringProcess.byteToString(Tx);
	dbgmessage.save(TmpString);
}
				while((Tx=comport.setTxBuf(Tx,Tx.length))!=null){};





			}
			//_RX_---------------------------------------------
			else if(db[index][_RX_].length()!=0){
if((dbg&_DBGTxRx_)==_DBGTxRx_){
	TmpString="\tdb["+index+"][_RX_]\t:"+db[index][_RX_];
	dbgmessage.save(TmpString);
}
				//_Padding_------------------------------------	
				if(db[index][_MaxLen_].length()!=0){
					if(Integer.parseInt(db[index][_MaxLen_])>(db[index][_RX_].length()/2)){
						TmpInt=Integer.parseInt(db[index][_MaxLen_])-(db[index][_RX_].length()/2);
						for(int n=0;n<TmpInt;n++){
							db[index][_RX_]+=db[index][_Padding_];
						}

					}
				}

				Rx=stringProcess.stringToByte(db[index][_RX_]);

if((dbg&_DBGTxRx_)==_DBGTxRx_){
	TmpString="\t\tRx\t:";
	TmpString+=stringProcess.byteToString(Rx);
	dbgmessage.save(TmpString);
}
				oldtime=(int)System.currentTimeMillis();

				do{
					TmpRx=comport.getRxBuf(0x00);
					//comport.reset();
					if(TmpRx.length>0){

						TmpString=stringProcess.byteToString(TmpRx);
					}


				}
				while(	((int)System.currentTimeMillis()-oldtime)<Integer.parseInt(db[index][_TimeOut_])
					&&	(!db[index][_RX_].equals(TmpString))	);



				//check ok-------------------------------------	
				oldString="";
				if(db[index][_RX_].equals(TmpString)){
					if((dbg&_DBGUARTTxRx_)==_DBGUARTTxRx_){
						TmpString="["+index+"]\t\tUART Rx:";
						TmpString+=db[index][_RX_];
						TmpString+="  (_OK_)";
						dbgmessage.save(TmpString);
					}

					if(db[index][_RepeatOk_].length()!=0){

						if(Integer.parseInt(db[index][_RepeatOk_])>0){
							if(Integer.parseInt(db[index][_RepeatOk_])>0){
								if(db[index][_JmpOkFirst_].length()!=0){
									oldString=db[index][_JmpOkFirst_];
								}
							}
							else{
								if(db[index][_JmpOkLast_].length()!=0){
									oldString=db[index][_JmpOkLast_];
								}
							}

							db[index][_RepeatOk_]=String.valueOf(Integer.parseInt(db[index][_RepeatOk_])-1);
						}
					}

				}
				//check fail-------------------------------------
				else{
					if((dbg&_DBGUARTTxRx_)==_DBGUARTTxRx_){
						TmpString="["+index+"]\t\tUART Rx:";
						TmpString+=db[index][_RX_];
						TmpString+=("  (_TimeOut_:"+stringProcess.byteToString(TmpRx)+")");
						dbgmessage.save(TmpString);
					}	

					if(db[index][_RepeatFail_].length()!=0){

						if(Integer.parseInt(db[index][_RepeatFail_])>0){
							if(Integer.parseInt(db[index][_RepeatFail_])>0){
								if(db[index][_JmpFailFirst_].length()!=0){
									oldString=db[index][_JmpFailFirst_];
								}
							}
							else{
								if(db[index][_JmpFailLast_].length()!=0){
									oldString=db[index][_JmpFailLast_];
								}
							}

							db[index][_RepeatFail_]=String.valueOf(Integer.parseInt(db[index][_RepeatFail_])-1);
						}
					}		
				}

				//force jmp-------------------------------------
				if(db[index][_Jmp_].length()!=0){
					oldString=db[index][_Jmp_];
				}

				//find next index-------------------------------------
				if(oldString.length()!=0){
					for(int m=0;m<allString.length;m++){
						if(oldString.equals(db[m][_Tag_])){
							tmpindex=m;
						}
					}
				}

			}
			//_DelayLast_-------------------------------------
			oldtime=(int)System.currentTimeMillis();
			while(((int)System.currentTimeMillis()-oldtime)<Integer.parseInt(db[index][_DelayLast_]));


			index=tmpindex;
		}
		while(index < (allString.length));
*/

//-----------------------------------------------------------------------------------------------------
	}
	catch(ArrayIndexOutOfBoundsException e){
		e.printStackTrace();
	}
//	catch(IOException e){
//		e.printStackTrace();
//	}
	catch( Exception e ){
		e.printStackTrace();
	}  


    }  

}  